<?php
/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

require_once (dirname(__file__) . '/Dictionary.php');
require_once (dirname(__file__) . '/IParameterHolder.php');

/**
 * Parameter Holder.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @package Simplify_Kernel
 */
class ParameterHolder extends Dictionary implements IParameterHolder
{

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/Dictionary#get($name, $default, $defaultIfEmpty)
   */
  public function get($name, $default = null, $filter = null)
  {
    $getter = 'get_' . $name;
    if (method_exists($this, $getter)) {
      return $this->$getter($default, $filter);
    }
    
    return parent::get($name, $default, $filter);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/IParameterHolder#getDefault($value, $default, $filter)
   */
  public function getDefault($value, $default, $filter = null)
  {
    if ($filter == Dictionary::FILTER_NULL && is_null($value))
      return $default;
    if ($filter == Dictionary::FILTER_EMPTY && empty($value))
      return $default;
    return $value;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/Dictionary#set($name, $value)
   */
  public function set($name, $value = null)
  {
    $setter = 'set_' . $name;
    if (method_exists($this, $setter)) {
      $this->$setter($value);
    }
    else {
      parent::set($name, $value);
    }
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/IParameterHolder#setDefault($name, $value, $filter)
   */
  public function setDefault($name, $value, $filter = null)
  {
    if (! $this->has($name, $filter))
      $this->set($name, $value);
  }

  /**
   * Parse $data as a Parameter Holder.
   *
   * @param mixed $data $data for the Parameter Holder.
   * @return ParameterHolder
   */
  public static function parseFrom($data)
  {
    if (is_a($data, 'ParameterHolder'))
      return $data;
    if (method_exists($data, 'getAll'))
      $data = $data->getAll();
    return new ParameterHolder($data);
  }

  /**
   * Internal getter. Ignores getters.
   * @see simplify/kernel/data/Dictionary#get($name, $default, $defaultIfEmpty)
   */
  protected function _get($name, $default = null, $defaultIfEmpty = false)
  {
    return parent::get($name, $default, $defaultIfEmpty);
  }

  /**
   * Internal setter. Ignores setters.
   * @see simplify/kernel/data/Dictionary#set($name, $value)
   */
  protected function _set($name, $value = null)
  {
    parent::set($name, $value);
  }

}

?>